package com.thb.pay.agent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.thb.pay.agent.entity.AgentChannel;
import com.thb.pay.agent.mapper.AgentChannelMapper;
import com.thb.pay.agent.service.AgentChannelService;
import com.thb.pay.api.agent.vo.req.AgentChannelReq;
import com.thb.pay.api.agent.vo.resp.AgentChannelVo;
import com.thb.pay.api.payment.vo.req.AgentPassagePageReq;
import com.thb.pay.core.utils.PageResult;
import com.thb.pay.db.service.impl.BaseServiceImpl;
import com.thb.pay.db.utils.PageResultUtils;
import com.thb.pay.db.utils.PageUtils;
import com.thb.pay.db.utils.QueryUtils;
import com.xiaoleilu.hutool.bean.BeanUtil;
import com.xiaoleilu.hutool.collection.CollectionUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * <p>
 * 代理商-通道表 服务实现类
 * </p>
 *
 * @author lucky
 * @since 2019-07-21
 */
@Service
public class AgentChannelServiceImpl extends BaseServiceImpl<AgentChannelMapper, AgentChannel> implements AgentChannelService {

    @Override
    public PageResult<AgentChannelVo> findPage(AgentPassagePageReq pageReq) {
        IPage<AgentChannel> currentPage = PageUtils.buildPage(pageReq);
        QueryWrapper wrapper = QueryUtils.buildQueryPage(pageReq);
        IPage<AgentChannel> pageResult = super.page(currentPage, wrapper);
        return PageResultUtils.build(pageResult, AgentChannelVo.class);
    }

    @Override
    public void add(AgentChannelReq agentChannelReq) {
        Assert.notNull(agentChannelReq, "agentChannelReq.not.null");
        Assert.notNull(agentChannelReq.getAgentId(), "agentId.not.null");
        Assert.notNull(agentChannelReq.getProductId(), "productId.not.null");
        AgentChannel agentChannel = new AgentChannel();
        BeanUtil.copyProperties(agentChannelReq, agentChannel);
        super.insert(agentChannel);
    }

    @Override
    public void update(AgentChannelReq agentChannelReq) {
        Assert.notNull(agentChannelReq, "agentChannelReq.not.null");
        Assert.notNull(agentChannelReq.getId(), "agentPassageId.not.null");
        AgentChannel agentChannel = super.selectById(agentChannelReq.getId());
        Assert.notNull(agentChannel, "agentChannel.not.null");
        AgentChannel targetAgentChannel = new AgentChannel();
        BeanUtil.copyProperties(agentChannelReq, targetAgentChannel);
        super.updateById(targetAgentChannel);
    }

    @Override
    public AgentChannelVo getById(Long id) {
        AgentChannel agentChannel = super.selectById(id);
        if (agentChannel != null) {
            AgentChannelVo agentChannelVo = new AgentChannelVo();
            BeanUtil.copyProperties(agentChannel, agentChannelVo);
            return agentChannelVo;
        }
        return null;
    }

    @Override
    public AgentChannelVo getByAgentIdAndProductId(Long agentId, Long productId) {
        LambdaQueryWrapper<AgentChannel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AgentChannel::getAgentId, agentId);
        queryWrapper.eq(AgentChannel::getProductId, productId);
        List<AgentChannel> resResultList = super.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(resResultList)) {
            AgentChannel agentChannel = resResultList.get(0);
            AgentChannelVo agentChannelVo = new AgentChannelVo();
            BeanUtil.copyProperties(agentChannel, agentChannelVo);
            return agentChannelVo;
        }
        return null;
    }

    @Override
    public List<AgentChannelVo> findAllList(AgentChannelReq conditionParam) {
        List<AgentChannelVo> targetResultList = Lists.newArrayList();
        LambdaQueryWrapper<AgentChannel> queryWrapper = new LambdaQueryWrapper<>();
        if (conditionParam != null) {
            if (conditionParam.getAgentId() != null) {
                queryWrapper.eq(AgentChannel::getAgentId, conditionParam.getAgentId());
            }
            if (conditionParam.getProductId() != null) {
                queryWrapper.eq(AgentChannel::getProductId, conditionParam.getProductId());
            }
            if (conditionParam.getStatus() != null && conditionParam.getStatus() != -99) {
                queryWrapper.eq(AgentChannel::getStatus, conditionParam.getStatus());
            }
        }
        List<AgentChannel> resResultList = super.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(resResultList)) {
            resResultList.forEach(agentPassage -> {
                AgentChannelVo agentChannelVo = new AgentChannelVo();
                BeanUtil.copyProperties(agentPassage, agentChannelVo);
                targetResultList.add(agentChannelVo);
            });
        }
        return targetResultList;
    }

    @Override
    public List<AgentChannelVo> findAllByAgentId(Long agentId) {
        List<AgentChannelVo> targetResultList = Lists.newArrayList();
        LambdaQueryWrapper<AgentChannel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AgentChannel::getAgentId, agentId);
        List<AgentChannel> resResultList = super.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(resResultList)) {
            resResultList.forEach(agentPassage -> {
                AgentChannelVo agentChannelVo = new AgentChannelVo();
                BeanUtil.copyProperties(agentPassage, agentChannelVo);
                targetResultList.add(agentChannelVo);
            });
        }
        return targetResultList;
    }
}
